home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / sozobon / termcap.c < prev   
C/C++ Source or Header  |  1992-07-07  |  17KB  |  727 lines

  1. /* Work-alike for termcap, plus extra features.
  2.    Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22. /* BUFSIZE is the initial size allocated for the buffer
  23.    for reading the termcap file.
  24.    It is not a limit.
  25.    Make it large normally for speed.
  26.    Make it variable when debugging, so can exercise
  27.    increasing the space dynamically.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <termcap.h>
  33. #include <unistd.h>
  34.  
  35. #ifndef BUFSIZE
  36. #ifdef DEBUG
  37. #define BUFSIZE bufsize
  38. size_t bufsize = 128;
  39. #else
  40. #define BUFSIZE 2048
  41. #endif
  42. #endif
  43.  
  44. static void
  45. memory_out()
  46. {
  47.     static char msg[] = "Out of memory.\n";
  48.     write(2, msg, strlen(msg));
  49.     exit(1);
  50. }
  51.  
  52. static void *
  53. xmalloc(size)
  54.     size_t size;
  55. {
  56.     register void *tem = malloc(size);
  57.  
  58.     if (!tem)
  59.     memory_out();
  60.     return tem;
  61. }
  62.  
  63. static void *
  64. xrealloc(ptr, size)
  65.     void *ptr;
  66.     size_t size;
  67. {
  68.     register void *tem = realloc(ptr, size);
  69.  
  70.     if (!tem)
  71.     memory_out();
  72.     return tem;
  73. }
  74.  
  75. /* Looking up capabilities in the entry already found */
  76.  
  77. /* The pointer to the data made by tgetent is left here
  78.    for tgetnum, tgetflag and tgetstr to find.  */
  79.  
  80. static char *term_entry;
  81.  
  82. static char *tgetst1();
  83.  
  84. /* This is the main subroutine that is used to search
  85.    an entry for a particular capability */
  86.  
  87. static char *
  88. find_capability(bp, cap)
  89.     register char *bp,
  90.        *cap;
  91. {
  92.     for (; *bp; bp++)
  93.     if (bp[0] == ':'
  94.         && bp[1] == cap[0]
  95.         && bp[2] == cap[1])
  96.         return &bp[4];
  97.     return 0;
  98. }
  99.  
  100. int
  101. tgetnum(cap)
  102.     char *cap;
  103. {
  104.     register char *ptr = find_capability(term_entry, cap);
  105.  
  106.     if (!ptr || ptr[-1] != '#')
  107.     return -1;
  108.     return atoi(ptr);
  109. }
  110.  
  111. int
  112. tgetflag(cap)
  113.     char *cap;
  114. {
  115.     register char *ptr = find_capability(term_entry, cap);
  116.  
  117.     return 0 != ptr && ptr[-1] == ':';
  118. }
  119.  
  120. /* Look up a string-valued capability `cap'.
  121.    If `area' is nonzero, it points to a pointer to a block in which
  122.    to store the string.  That pointer is advanced over the space used.
  123.    If `area' is zero, space is allocated with `malloc'.  */
  124.  
  125. char *
  126. tgetstr(cap, area)
  127.     char *cap;
  128.     char **area;
  129. {
  130.     register char *ptr = find_capability(term_entry, cap);
  131.  
  132.     if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
  133.     return 0;
  134.     return tgetst1(ptr, area);
  135. }
  136.  
  137. /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
  138.    gives meaning of character following \, or a space if no special meaning.
  139.    Eight characters per line within the string.  */
  140.  
  141. static char esctab[]
  142. = " \007\010  \033\014 \
  143.       \012 \
  144.   \015 \011 \013 \
  145.         ";
  146.  
  147. /* Given a pointer to a string value inside a termcap entry (`ptr'),
  148.    copy the value and process \ and ^ abbreviations.
  149.    Copy into block that *area points to,
  150.    or to newly allocated storage if area is 0.  */
  151.  
  152. static char *
  153. tgetst1(ptr, area)
  154.     char *ptr;
  155.     char **area;
  156. {
  157.     register char *p,
  158.        *r;
  159.     register int c;
  160.     register int c1;
  161.     register size_t size;
  162.     char *ret;
  163.  
  164.     if (!ptr)
  165.     return 0;
  166.  
  167.     /* `ret' gets address of where to store the string */
  168.     if (!area) {
  169.     /* Compute size of block needed (may overestimate) */
  170.     p = ptr;
  171.     while ((c = *p++) && c != ':' && c != '\n');
  172.     ret = (char *) xmalloc(p - ptr + 1);
  173.     } else
  174.     ret = *area;
  175.  
  176.     /* Copy the string value, stopping at null or colon.  */
  177.     /* Also process ^ and \ abbreviations.  */
  178.     p = ptr;
  179.     r = ret;
  180.     while ((c = *p++) && c != ':' && c != '\n') {
  181.     if (c == '^')
  182.         c = *p++ & 037;
  183.     else if (c == '\\') {
  184.         c = *p++;
  185.         if (c >= '0' && c <= '7') {
  186.         c -= '0';
  187.         size = 0;
  188.  
  189.         while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7') {
  190.             c *= 8;
  191.             c += c1 - '0';
  192.             p++;
  193.         }
  194.         } else if (c >= 0100 && c < 0200) {
  195.         c1 = esctab[(c & ~040) - 0100];
  196.         if (c1 != ' ')
  197.             c = c1;
  198.         }
  199.     }
  200.     *r++ = c;
  201.     }
  202.     *r = 0;
  203.     /* Update *area */
  204.     if (area)
  205.     *area = r + 1;
  206.     return ret;
  207. }
  208.  
  209. /* Outputting a string with padding */
  210.  
  211. short ospeed;
  212. char PC;
  213.  
  214. /* Actual baud rate if positive; - baud rate / 100 if negative. */
  215.  
  216. static short speeds[] =
  217. {
  218.  0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
  219.  -18, -24, -48, -96, -192, -384
  220. };
  221.  
  222. void
  223. tputs(string, nlines, outfun)
  224.     register char *string;
  225.     int nlines;
  226.     register int (*outfun)();
  227. {
  228.     register int padcount = 0;
  229.  
  230.     if (!string)
  231.     return;
  232.     while (*string >= '0' && *string <= '9') {
  233.     padcount += *string++ - '0';
  234.     padcount *= 10;
  235.     }
  236.     if (*string == '.') {
  237.     string++;
  238.     padcount += *string++ - '0';
  239.     }
  240.     if (*string == '*') {
  241.     string++;
  242.     padcount *= nlines;
  243.     }
  244.     while (*string)
  245.     (*outfun) (*string++);
  246.  
  247.     /* padcount is now in units of tenths of msec.  */
  248.     padcount *= speeds[ospeed];
  249.     padcount += 500;
  250.     padcount /= 1000;
  251.     if (speeds[ospeed] < 0)
  252.     padcount = -padcount;
  253.     else {
  254.     padcount += 50;
  255.     padcount /= 100;
  256.     }
  257.  
  258.     while (padcount-- > 0)
  259.     (*outfun) (PC);
  260. }
  261.  
  262. /* Finding the termcap entry in the termcap data base */
  263.  
  264. struct buffer {
  265.     char *beg;
  266.     size_t size;
  267.     char *ptr;
  268.     int ateof;
  269.     int full;
  270. };
  271.  
  272. /* Forward declarations of static functions */
  273.  
  274. static int scan_file();
  275. static char *gobble_line();
  276. static int compare_contin();
  277. static int name_match();
  278.  
  279. /* Find the termcap entry data for terminal type `name'
  280.    and store it in the block that `bp' points to.
  281.    Record its address for future use.
  282.  
  283.    If `bp' is zero, space is dynamically allocated.  */
  284. /* 4/19/92 sb -- as per Steve Yelvingon's advice, tgetent() has been changed
  285.    to return int instead of (char *). */
  286.  
  287. int
  288. tgetent(bp, name)
  289.     char *bp,
  290.        *name;
  291. {
  292.     register char *tem;
  293.     register int fd;
  294.     struct buffer buf;
  295.     register char *bp1;
  296.     char *bp2;
  297.     char *term;
  298.     size_t malloc_size = 0;
  299.     register int c;
  300.     char *tcenv;        /* TERMCAP value, if it contais :tc=.  */
  301.     char *indirect = 0;        /* Terminal type in :tc= in TERMCAP value.  */
  302.     int filep;
  303.  
  304.     tem = (char *) getenv("TERMCAP");
  305.     if (tem && *tem == 0)
  306.     tem = 0;
  307.  
  308.     filep = tem && (*tem == '/');
  309.  
  310.     /*
  311.      * If tem is non-null and starts with / (in the un*x case, that is), it
  312.      * is a file name to use instead of /etc/termcap. If it is non-null and
  313.      * does not start with /, it is the entry itself, but only if the name
  314.      * the caller requested matches the TERM variable.
  315.      */
  316.  
  317.     if (tem && !filep && !strcmp(name, getenv("TERM"))) {
  318.     indirect = tgetst1(find_capability(tem, "tc"), (char *) 0);
  319.     if (!indirect) {
  320.         if (!bp)
  321.         bp = tem;
  322.         else
  323.         strcpy(bp, tem);
  324.         goto ret;
  325.     } else {        /* we will need to read /etc/termcap */
  326.         tcenv = tem;
  327.         tem = 0;
  328.     }
  329.     } else
  330.     indirect = 0;
  331.  
  332.     if (!tem)
  333.     tem = "/etc/termcap";
  334.  
  335.     /* Here we know we must search a file and tem has its name.  */
  336.  
  337.     fd = open(tem, 0, 0);
  338.     if (fd < 0)
  339.     return -1;
  340.  
  341.     buf.size = BUFSIZE;
  342.     /* Add 1 to size to ensure room for terminating null.  */
  343.     buf.beg = (char *) xmalloc(buf.size + 1);
  344.     term = indirect ? indirect : name;
  345.  
  346.     if (!bp) {
  347.     malloc_size = indirect ? strlen(tcenv) + 1 : buf.size;
  348.     bp = (char *) xmalloc(malloc_size);
  349.     }
  350.     bp1 = bp;
  351.  
  352.     if (indirect) {        /* copy the data from the environment
  353.                  * variable */
  354.     strcpy(bp, tcenv);
  355.     bp1 += strlen(tcenv);
  356.     }
  357.     while (term) {
  358.     /* Scan file, reading it via buf, till find start of main entry */
  359.     if (scan_file(term, fd, &buf) == 0)
  360.         return 0;
  361.  
  362.     /* Free old `term' if appropriate.  */
  363.     if (term != name)
  364.         free(term);
  365.  
  366.     /* If `bp' is malloc'd by us, make sure it is big enough.  */
  367.     if (malloc_size) {
  368.         malloc_size = bp1 - bp + buf.size;
  369.         tem = (char *) xrealloc(bp, malloc_size);
  370.         bp1 += tem - bp;
  371.         bp = tem;
  372.     }
  373.     bp2 = bp1;
  374.  
  375.     /* Copy the line of the entry from buf into bp.  */
  376.     tem = buf.ptr;
  377. #ifdef __SOZOBON__
  378.     /* Allow for "\n" or "\r\n" as newline sequence */
  379.     while ((*bp1++ = c = *tem++) && c != '\n' && c != '\r')
  380.         /* Drop out any \ newline sequence. */
  381.         if (c == '\\' && (*tem == '\n' || *tem == '\r')) {
  382.         bp1--;
  383.         if (*tem == '\r')
  384.             tem += 2;
  385.         else
  386.             tem++;
  387.         }
  388. #else
  389.     while ((*bp1++ = c = *tem++) && c != '\n')
  390.         /* Drop out any \ newline sequence. */
  391.         if (c == '\\' && *tem == '\n') {
  392.         bp1--;
  393.         tem++;
  394.         }
  395. #endif
  396.     *bp1 = 0;
  397.  
  398.     /* Does this entry refer to another terminal type's entry?  */
  399.     /* If something is found, copy it into heap and null-terminate it */
  400.     term = tgetst1(find_capability(bp2, "tc"), 0);
  401.     }
  402.  
  403.     close(fd);
  404.     free(buf.beg);
  405.  
  406.     if (malloc_size) {
  407.     bp = (char *) xrealloc(bp, bp1 - bp + 1);
  408.     }
  409. ret:
  410.     term_entry = bp;
  411.     if (malloc_size)
  412.     return bp;
  413.     return 1;
  414. }
  415.  
  416. #ifdef __SOZOBON__
  417. /* Allow for "\n" or "\r\n" as end-of-line sequences */
  418. #define is_contline(E) \
  419.     (E[-2]=='\\' || ((E[-2]=='\r' || E[-2]=='\n') && E[-3]=='\\'))
  420. #else
  421. #define is_contline(E) (E[-2]=='\\')
  422. #endif
  423.  
  424. /* Given file open on `fd' and buffer `bufp',
  425.    scan the file from the beginning until a line is found
  426.    that starts the entry for terminal type `string'.
  427.    Returns 1 if successful, with that line in `bufp',
  428.    or returns 0 if no entry found in the file.  */
  429.  
  430. static int
  431. scan_file(string, fd, bufp)
  432.     char *string;
  433.     int fd;
  434.     register struct buffer *bufp;
  435. {
  436.     register char *tem;
  437.     register char *end;
  438.  
  439.     bufp->ptr = bufp->beg;
  440.     bufp->full = 0;
  441.     bufp->ateof = 0;
  442.     *bufp->ptr = 0;
  443.  
  444.     lseek(fd, 0L, 0);
  445.  
  446.     while (!bufp->ateof) {
  447.     /* Read a line into the buffer */
  448.     end = 0;
  449.     do {
  450.         /*
  451.          * if it is continued, append another line to it, until a
  452.          * non-continued line ends
  453.          */
  454.         end = gobble_line(fd, bufp, end);
  455.     }
  456.     while (!bufp->ateof && is_contline(end));
  457.  
  458.     if (*bufp->ptr != '#'
  459.         && name_match(bufp->ptr, string))
  460.         return 1;
  461.  
  462.     /* Discard the line just processed */
  463.     bufp->ptr = end;
  464.     }
  465.     return 0;
  466. }
  467.  
  468. /* Return nonzero if NAME is one of the names specified
  469.    by termcap entry LINE.  */
  470.  
  471. static int
  472. name_match(line, name)
  473.     char *line,
  474.        *name;
  475. {
  476.     register char *tem;
  477.  
  478.     if (!compare_contin(line, name))
  479.     return 1;
  480.     /* This line starts an entry.  Is it the right one?  */
  481.     for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++)
  482.     if (*tem == '|' && !compare_contin(tem + 1, name))
  483.         return 1;
  484.  
  485.     return 0;
  486. }
  487.  
  488. static int
  489. compare_contin(str1, str2)
  490.     register char *str1,
  491.        *str2;
  492. {
  493.     register int c1,
  494.         c2;
  495.  
  496.     while (1) {
  497.     c1 = *str1++;
  498.     c2 = *str2++;
  499.     while (c1 == '\\' && *str1 == '\n') {
  500.         str1++;
  501.         while ((c1 = *str1++) == ' ' || c1 == '\t');
  502.     }
  503.     if (c2 == '\0') {    /* end of type being looked up */
  504.         if (c1 == '|' || c1 == ':')    /* If end of name in data base, */
  505.         return 0;    /* we win. */
  506.         else
  507.         return 1;
  508.     } else if (c1 != c2)
  509.         return 1;
  510.     }
  511. }
  512.  
  513. /* Make sure that the buffer <- `bufp' contains a full line
  514.    of the file open on `fd', starting at the place `bufp->ptr'
  515.    points to.  Can read more of the file, discard stuff before
  516.    `bufp->ptr', or make the buffer bigger.
  517.  
  518.    Returns the pointer to after the newline ending the line,
  519.    or to the end of the file, if there is no newline to end it.
  520.  
  521.    Can also merge on continuation lines.  If `append_end' is
  522.    nonzero, it points past the newline of a line that is
  523.    continued; we add another line onto it and regard the whole
  524.    thing as one line.  The caller decides when a line is continued.  */
  525.  
  526. static char *
  527. gobble_line(fd, bufp, append_end)
  528.     int fd;
  529.     register struct buffer *bufp;
  530.     char *append_end;
  531. {
  532.     register char *end;
  533.     register size_t nread;
  534.     register char *buf = bufp->beg;
  535.     register char *tem;
  536.  
  537.     if (append_end == 0)
  538.     append_end = bufp->ptr;
  539.  
  540.     for (;;) {
  541.     end = append_end;
  542.     while (*end && *end != '\n')
  543.         end++;
  544.     if (*end)
  545.         break;
  546.     if (bufp->ateof)
  547.         return buf + bufp->full;
  548.     if (bufp->ptr == buf) {
  549.         if (bufp->full == bufp->size) {
  550.         bufp->size *= 2;
  551.         /* Add 1 to size to ensure room for terminating null.  */
  552.         tem = (char *) xrealloc(buf, bufp->size + 1);
  553.         bufp->ptr = (bufp->ptr - buf) + tem;
  554.         append_end = (append_end - buf) + tem;
  555.         bufp->beg = buf = tem;
  556.         }
  557.     } else {
  558.         append_end -= bufp->ptr - buf;
  559.         bcopy(bufp->ptr, buf, bufp->full -= bufp->ptr - buf);
  560.         bufp->ptr = buf;
  561.     }
  562.     if (!(nread = read(fd, buf + bufp->full, bufp->size - bufp->full)))
  563.         bufp->ateof = 1;
  564.     bufp->full += nread;
  565.     buf[bufp->full] = 0;
  566.     }
  567.     return end + 1;
  568. }
  569.  
  570. /* 6/9/92 sb -- The tgoto() function seems to have been left out, so I'm
  571.    adding it.  It's as general as I can make it, doing full %-processing,
  572.    at least to the extent that I understand how it should be done
  573.    [\- and ^-processing should already have been done by tgetstr()].
  574. */
  575. char *
  576. tgoto(cm, destcol, destline)
  577.     char *cm;
  578.     int destcol,destline;
  579. {
  580.     static char buf[64];    /* if this isn't enough, we're in trouble */
  581.     register char *t = cm;
  582.     register char *s = buf;
  583.     char *r;
  584.     int vals[3], n=1, i;
  585.     int uf = 0, lf = 0;
  586.     char c;
  587.  
  588.     vals[0] = vals[2] = destcol;
  589.     vals[1] = destline;
  590.     while (*t) {
  591.     if (*t != '%')
  592.         *s++ = *t;
  593.     else switch (*++t) {
  594.         case '%':        /* escaped '%' */
  595.         *s++ = '%';
  596.         break;
  597.         case '3':        /* print as 3-digit integer */
  598.         *s++ = (vals[n] % 1000) / 100 + '0';
  599.         /* and fall through */
  600.         case '2':        /* print as 2-digit integer */
  601.         *s++ = (vals[n] % 100) / 10 + '0';
  602.         /* and fall through */
  603.         case 'd':        /* print as 1-digit integer */
  604.         *s++ = (vals[n++] % 10) + '0';
  605.         break;
  606.         case '>':        /* print as char with conditional offset */
  607.         if (vals[n] <= *++t) {
  608.             t++;
  609.             goto skipme;
  610.         }        /* otherwise, fall through */
  611.         case '+':        /* print as character value with offset */
  612.         vals[n] += *++t;
  613.                 /* and fall through */
  614.         case '.':        /* print as character value */
  615.         skipme:
  616.         c = vals[n];
  617.                 /* check for dangerous characters */
  618.         if (c == '\0' || c == '\004' || c == '\t' | c == '\n'
  619.                   || c == '\r')
  620.         {
  621.             c++;
  622.                 /* we now need to move up or left, depending
  623.                    on which coord we were processing; set a
  624.                    flag to remind ourselves */
  625.             if (n == 1)
  626.             uf = 1;
  627.             if (n != 1);
  628.             lf = 1;
  629.         }
  630.         *s++ = c;
  631.         n++;
  632.         break;
  633.     /* the exotic ones */
  634.         case 'r':        /* reverse row and col */
  635.         n = 0;        /* the whole point behind the vals[] array */
  636.         break;
  637.         case 'i':        /* add 1 to row and col */
  638.         vals[0]++;
  639.         vals[1]++;
  640.         vals[2]++;
  641.         break;
  642.         case 'n':        /* xor row & col with 0140 (DM2500) */
  643.         vals[0] ^= 0140;
  644.         vals[1] ^= 0140;
  645.         vals[2] ^= 0140;
  646.         break;
  647.         case 'B':        /* BCD encoding */
  648.         for (i=0; i<2; i++)
  649.             vals[i] = (vals[i] / 10) * 16 + (vals[i] % 10);
  650.         break;
  651.         case 'D':        /* reverse encoding (Delta Data) */
  652.         for (i=0; i<2; i++)
  653.             vals[i] -= 2 * (vals[i] & 0x0f);
  654.         break;
  655.     }
  656.     t++;
  657.     }
  658.     r = s;            /* can't pass &s to tgetstr(), now can we? */
  659.     if (uf)            /* need to go up */
  660.     tgetstr("up", &r);
  661.     if (lf) {            /* need to go left */
  662.     if (tgetflag("bs"))    /* is ^H the backspace character? */
  663.         *r++ = '\010';
  664.     else
  665.         tgetstr("bc", &r);    /* if not, find out what is */
  666.     }
  667.     *r = '\0';
  668.     return buf;
  669. }
  670.  
  671. #ifdef TEST
  672.  
  673. main(argc, argv)
  674.     int argc;
  675.     char **argv;
  676. {
  677.     char *term;
  678.     char *buf;
  679.  
  680.     term = argv[1];
  681.     printf("TERM: %s\n", term);
  682.  
  683. #if 0
  684.     buf = (char *) tgetent((char *) 0, term);
  685.     if ((long) buf <= 0) {
  686.     printf("No entry.\n");
  687.     return 0;
  688.     }
  689.     
  690.     printf("Entry: %s\n", buf);
  691. #else
  692.     if (tgetent((char *) 0, term) <= 0) {
  693.     printf("No entry.\n");
  694.     return 0;
  695.     }
  696.     
  697.     printf("Entry: %s\n", term_entry);
  698. #endif
  699.  
  700.     tprint("cm");
  701.     tprint("AL");
  702.  
  703.     printf("co: %d\n", tgetnum("co"));
  704.     printf("am: %d\n", tgetflag("am"));
  705. }
  706.  
  707. tprint(cap)
  708.     char *cap;
  709. {
  710.     char *x = tgetstr(cap, (char *) 0);
  711.     register char *y;
  712.  
  713.     printf("%s: ", cap);
  714.     if (x) {
  715.     for (y = x; *y; y++)
  716.         if (*y <= ' ' || *y == 0177)
  717.         printf("\\%0o", *y);
  718.         else
  719.         putchar(*y);
  720.     free(x);
  721.     } else
  722.     printf("none");
  723.     putchar('\n');
  724. }
  725.  
  726. #endif                /* TEST */
  727.